#include "wave.h"
#include "intrins.h"

sbit TX = P1^0;
sbit RX = P1^1;


void Delay12us(void)	//@12.000MHz
{
	unsigned char data i;

	_nop_();
	i = 3;
	while (--i);
}



void wave_init()
{
	uc i;
	EA = 0;
	for(i=0;i<8;i++)
	{
		TX = 1;
		Delay12us();
		TX = 0;
		Delay12us();
	}
	EA = 1;
}

unsigned int wave_run()
{
	unsigned int time;
	CMOD = 0X00;
	CH = CL = 0;
	wave_init();
	CR = 1;
	while((CF == 0)&&(RX == 1))
		;
	CR = 0;
	if(CF == 0)
	{
		time = CH<<8|CL;
		return (unsigned int)(time*0.017);
	}
	else
	{
		CF = 0;
		return 0;
	}
}
